package com.peng.leetcode.other;

/**
 * LongestCommonSubsequence
 *
 * @author: lupeng6
 * @create: 2021/4/6 15:05
 */
public class LongestCommonSubsequence {

    public static void main(String[] args) {
        String text1 = "abcde";
        String text2 = "ace";
        System.out.println(new LongestCommonSubsequence().longestCommonSubsequence(text1, text2));
    }

    public int longestCommonSubsequence(String text1, String text2) {
        int answer = 0;
        int index1 = 0;
        int index2 = 0;

        while (index1 < text1.length()) {
            char c = text1.charAt(index1);
            while (index2 < text2.length() && text2.charAt(index2) != c) {
                index2++;
            }
            if (text2.charAt(index2) == c) {
                answer++;
            }
            index1++;
        }

        return answer;
    }
}
